package com.tantou.java.structandalgorithm.sort;

import java.util.Arrays;

/**
 * 选择排序
 * https://www.toutiao.com/article/7180246230860366393/?app=news_article&group_id=7180246230860366393&req_id=20240408111443CA759CCBE621096E6A44&share_token=75a37611-d2db-4faa-aeef-d6cdd87713c4&timestamp=1712546084&tt_from=copy_link&use_new_style=1&utm_campaign=client_share&utm_medium=toutiao_android&utm_source=copy_link&source=m_redirect
 */
public class SelectSortTest {

    public static void main(String[] args) {
        int[] nums = {2, 5, 6, 4, 3, 7};
        nums = sort(nums);
        System.out.println(Arrays.toString(nums));
    }

    public static int[] sort(int[] nums) {

        if (nums.length < 1) {
            return nums;
        }

        for (int i = 0; i <= nums.length - 1; i++) {
            int min = nums[i];
            int minIndex = i;
            //从待排序中 选出最小的
            for (int j = i + 1; j <= nums.length - 1; j++) {
                if (nums[j] < min) {
                    min = nums[j];
                    minIndex = j;
                }
            }
            //一轮结束后，把最小值放到对应的位置
            int tmp = nums[i];
            nums[i] = nums[minIndex];
            nums[minIndex] = tmp;
        }
        return nums;
    }

}
